package com.ktgj.train.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.train.domain.TrainStationList;
import com.ktgj.train.domain.trainlist.CityList;
import com.ktgj.train.domain.vo.CityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.train.domain.TrainTrainstation;
import com.ktgj.train.service.ITrainTrainstationService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 火车站信息Controller
 * 
 * @author ruoyi
 * @date 2021-11-29
 */
@ControllerAdvice
@RestController
@RequestMapping("/trainstation")
public class TrainTrainstationController extends BaseController
{
    @Autowired
    private ITrainTrainstationService trainTrainstationService;

    /**
     * 查询火车站信息列表
     */
    @PreAuthorize(hasPermi = "train:trainstation:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainTrainstation trainTrainstation)
    {
        startPage();
        List<TrainTrainstation> list = trainTrainstationService.selectTrainTrainstationList(trainTrainstation);
        return getDataTable(list);
    }

    /**
     * 导出火车站信息列表
     */
    @PreAuthorize(hasPermi = "train:trainstation:export")
    @Log(title = "火车站信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainTrainstation trainTrainstation) throws IOException
    {
        List<TrainTrainstation> list = trainTrainstationService.selectTrainTrainstationList(trainTrainstation);
        ExcelUtil<TrainTrainstation> util = new ExcelUtil<TrainTrainstation>(TrainTrainstation.class);
        util.exportExcel(response, list, "火车站信息数据");
    }

    /**
     * 获取火车站信息详细信息
     */
    //@PreAuthorize(hasPermi = "train:trainstation:query")
    @GetMapping(value = "/{trainstationid}")
    public AjaxResult getInfo(@PathVariable("trainstationid") Long trainstationid)
    {
        return AjaxResult.success(trainTrainstationService.selectTrainTrainstationById(trainstationid));
    }

    /**
     * 新增火车站信息
     */
    @PreAuthorize(hasPermi = "train:trainstation:add")
    @Log(title = "火车站信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainTrainstation trainTrainstation)
    {
        return toAjax(trainTrainstationService.insertTrainTrainstation(trainTrainstation));
    }

    /**
     * 修改火车站信息
     */
    @PreAuthorize(hasPermi = "train:trainstation:edit")
    @Log(title = "火车站信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainTrainstation trainTrainstation)
    {
        return toAjax(trainTrainstationService.updateTrainTrainstation(trainTrainstation));
    }

    /**
     * 删除火车站信息
     */
    @PreAuthorize(hasPermi = "train:trainstation:remove")
    @Log(title = "火车站信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainstationids}")
    public AjaxResult remove(@PathVariable Long[] trainstationids)
    {
        return toAjax(trainTrainstationService.deleteTrainTrainstationByIds(trainstationids));
    }

    /**
     * 搜索
     * */
    @PostMapping("/search")
    public AjaxResult search(@RequestBody Map<String, Object> map){
        String name = map.get("name").toString();
        return AjaxResult.success(trainTrainstationService.selectTrainTrainstationVague(name));
    }

    @PostMapping("/queryLikeByStation")
    public AjaxResult queryLikeByStation(@RequestBody Map<String, Object> map) {
        String name = map.get("name").toString();
        List<TrainStationList> trainStationLists = trainTrainstationService.queryLikeByStation(name);
        if (trainStationLists != null) {
            return AjaxResult.success(trainStationLists);
        } else {
            return AjaxResult.error("查询失败,未找到此站!");
        }
    }

    @GetMapping("/IsHotTrain")
    public AjaxResult IsHotTrain() {
        List<TrainStationList> trainStationLists = trainTrainstationService.queryIsHot();
        if (trainStationLists != null) {
            return AjaxResult.success(trainStationLists);
        } else {
            return AjaxResult.error("获取信息失败");
        }
    }

    @GetMapping("/CityList")
    public AjaxResult CityList() {
        List<CityList> city = trainTrainstationService.queryCityList();
        if (city != null) {
            return AjaxResult.success(city);
        } else {
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 城市列表
     * */
    @PostMapping("/selectCityList")
    public AjaxResult selectCityList() {
        List<CityVo> cityVoList = trainTrainstationService.selectCityList();
        if (cityVoList != null && cityVoList.size() > 0){
            return AjaxResult.success(cityVoList);
        }
        else {
            return AjaxResult.error("没有数据");
        }
    }

    /**
     * 获取火车站三字码
     * */
    @PostMapping("/selectCityId")
    public String selectCityId(@RequestBody Map<String, Object> map) {
        String gaodename = map.get("gaodename").toString();
        TrainTrainstation trainTrainstation = trainTrainstationService.selectCityId(gaodename);
        return trainTrainstation.getTrainstationcode();
    }

    /**
     * 三字码获取城市名称
     * */
    @PostMapping("/selectCityName")
    public TrainTrainstation selectCityName(@RequestBody Map<String, Object> map){
        String code = map.get("code").toString();
        TrainTrainstation trainTrainstation = trainTrainstationService.selectCityName(code);
        return trainTrainstation;
    }

    /**
     * 获取城市代码、id、名称
     * */
    @PostMapping("/cityVo")
    public CityVo cityVo(@RequestBody CityVo cityVo){
        cityVo = trainTrainstationService.cityVo(cityVo);
        return cityVo;
    }
}
